package com.rech.xbexpress3;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.rech.utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Timer;
import java.util.TimerTask;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class Jj1Activity extends AppCompatActivity {



    private int jjcount = 100;
    private boolean mVisible;
    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    private Timer countTimer;
    private long mLastClickTime = 0;
    public static final long TIME_INTERVAL = 3000L;
    private TextView djs ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences("xbbox", Context.MODE_PRIVATE);
        String orientation = sharedPreferences.getString("Orientation","");
        if (orientation.equals("landscape")) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        setContentView(R.layout.activity_jj1);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mVisible = true;getSupportActionBar().hide();
        djs = findViewById(R.id.jj1_djs);

        // Upon interacting with UI controls, delay any scheduled hide()
        // operations to prevent the jarring behavior of controls going away
        ImageView cnqrcode = findViewById(R.id.cn_qrcode);
        ImageView kdqrcode = findViewById(R.id.kd_qrcode);
        String kdImage     =  sharedPreferences.getString("CabinetType_kuai","");
        String cnImage     =  sharedPreferences.getString("CabinetType_cai","");
        new FetchUrlImageTask(kdImage,kdqrcode).execute();
        new FetchUrlImageTask(cnImage,cnqrcode).execute();


        final ImageView jj1_cnbtn = (ImageView)findViewById(R.id.jj1_cnbtn);
        final ImageView jj1_kdbtn = (ImageView)findViewById(R.id.jj1_kdbtn);

        jj1_cnbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.step1).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 0));
                findViewById(R.id.step2).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 1));
                jj1_kdbtn.setBackgroundColor(Color.rgb(255, 255, 255));
                jj1_cnbtn.setBackgroundColor(R.drawable.edit_border);
            }
        });

        jj1_kdbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //jj1_cnbtn.setClickable(false);
                findViewById(R.id.step1).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 1));
                findViewById(R.id.step2).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 0));
                jj1_cnbtn.setBackgroundColor(Color.rgb(255, 255, 255));
                jj1_kdbtn.setBackgroundColor(R.drawable.edit_border);
            }
        });

        findViewById(R.id.step1).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 0));
        findViewById(R.id.step2).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 1));
        //jj1_kdbtn.setBackgroundColor(R.drawable.button_shape);
        jj1_cnbtn.setBackgroundColor(R.drawable.edit_border);

        final Button jj1_next = findViewById(R.id.jj1_next);
        jj1_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jj1_next.setClickable(false);
                long nowTime = System.currentTimeMillis();

                if (nowTime - mLastClickTime > TIME_INTERVAL) {
                    mLastClickTime = nowTime;
                    Intent intent = new Intent();
                    setResult(1008, intent);
                    Exit();
                    finish();
                }
            }
        });


        Button jj1_back = findViewById(R.id.jj1_back);
        jj1_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                setResult(4001, intent);
                Exit();finish();
            }
        });





        countTimer=new Timer();
        countTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                jjcount--;
                //Jj1Activity.this.djs.setText(String.valueOf(OtherUtil.jjcount));
                Message message = new Message();
                message.what=20000;
                EventBus.getDefault().post(message);
                if(jjcount < 1){
                    //OtherUtil.restartApplication(Jj1Activity.this);
                    Exit();
                    finish();
                }
            }
        },0,1000);
    }

    private void Exit(){
        countTimer.cancel();
        EventBus.getDefault().unregister(this);
    }
    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
    }

    private void toggle() {
        if (mVisible) {
            hide();
        } else {
            show();
        }
    }

    private void hide() {
        // Hide UI first
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        mVisible = false;

    }

    @SuppressLint("InlinedApi")
    private void show() {
        mVisible = true;

    }
    @Subscribe(threadMode = ThreadMode.MAIN,priority = 100,sticky = false)     //在UI线程执行，100  为优先级
    public void onEvent(Message message){
        Intent intent = new Intent();
        switch(message.what){
            case 20000:
                handleDJS();
                break;


        }
    }
    private void handleDJS(){
        djs.setText(String.valueOf(jjcount));
    }
}
